package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.utils.ScreenUtil;

public class About_LianxiFangshiActivity extends BaseAct {
	
	@ViewInject(R.id.lianxiimg)
	private ImageView m_ImageView;

	@ViewInject(R.id.tv_content)
	private TextView tv_content;

	@ViewInject(R.id.tv_web)
	private TextView tv_web;

	@Override
	protected int setView() {
		return R.layout.layout_about_lianxi;
	}

	@Override
	protected void init() {
		BitmapFactory.Options options = new BitmapFactory.Options();  
		options.inJustDecodeBounds = true;  
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.jianjie2, options);
		
		LayoutParams params = (LayoutParams) m_ImageView.getLayoutParams();
		
		int oldwidth = options.outWidth;
		int oldhight = options.outHeight;
		int newWidth = ScreenUtil.getInstant().getDisWidth(ctx);
		int newHight = (oldhight * newWidth )/oldwidth;
		params.width = newWidth;
		params.height = newHight;
		m_ImageView.setLayoutParams(params);

		String content = getString(R.string.lianxi);
		String str1 = "4006699178";
		String str2 = "0371-63213869";
		String str3 = "13538609930";

		SpannableStringBuilder style = new SpannableStringBuilder(content);
		int index1 = content.indexOf(str1);
		int index2 = content.indexOf(str2);
		int index3 = content.indexOf(str3);
		style.setSpan(new TestSpanClick(str1), index1,index1+str1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		style.setSpan(new TestSpanClick(str2), index2,index2+str2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		style.setSpan(new TestSpanClick(str3), index3,index3+str3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv_content.setMovementMethod(LinkMovementMethod.getInstance());
		tv_content.setText(style);

		style = new SpannableStringBuilder(getString(R.string.web));
		style.setSpan(new TestSpanClick(getString(R.string.web),true), 0,getString(R.string.web).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv_web.setMovementMethod(LinkMovementMethod.getInstance());
		tv_web.setText(style);
	}

	private class TestSpanClick extends ClickableSpan {
		private String str;
		private boolean isLink;

		public TestSpanClick(String str) {
			this.str = str;
		}
		public TestSpanClick(String str, boolean isLink) {
			this.str = str;
			this.isLink = isLink;
		}

		@Override
		public void onClick(View widget) {
			if(isLink){
				String url ="https://shop148995666.taobao.com/?spm=a230r.7195193.1997079397.2.ycnRDK&qq-pf-to=pcqq.c2c";
				try{
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(url));//设置一个URI地址
					startActivity(intent);//用startActivity打开这个指定的网页。
				}catch(Exception e){
					Uri uri = Uri.parse(url);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					intent.setClassName("com.android.browser",
							"com.android.browser.BrowserActivity");
					startActivity(intent);
				}
			}else{
				Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		}
	}
	@Override
	protected String setTopTitle() {
		return null;
	}

	@Override
	protected boolean topHide() {
		return true;
	}
}
